#coding=utf-8
from fastapi import Request,FastAPI,status 
 
from fastapi.encoders import jsonable_encoder
from starlette.middleware.base import BaseHTTPMiddleware
from fastapi.responses import JSONResponse
from typing import Any,Dict,Optional

"""
# 成功响应规范
# data : Any 响应主题数据
# msg : str|None 请求消息
# code : int|1 1为成功，-1或0为失败
"""
def success(data: Any=None, msg: Optional[str]="请求成功", code=1, ext: Any=None, status_code=status.HTTP_200_OK):
    return JSONResponse(
        status_code=status_code,
        content=jsonable_encoder({
            'data': data, 
            "msg": msg, 
            "code": code, 
            "ext": ext 
        })
    )

"""
# 成功响应规范
# data : Any 响应主题数据
# msg : str|None 请求消息
# code : int|1 1为成功，-1或0为失败
"""
def fail(msg: Optional[str]="请求失败", code=-1, ext: Any=None, status_code=status.HTTP_200_OK ):
    return JSONResponse(
        status_code=status_code,
        content=jsonable_encoder({
            'data': None, 
            "msg": msg, 
            "code": code, 
            "ext": ext 
        })
    )